import java.awt.*; 
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.applet.*; 
// We need Date to get the current time. 
import java.net.URL;
import java.util.Date;

// Implement Runnable, this will make it possible for threads 
// to call the run method when activated. 
    public class Game extends Applet implements KeyListener,MouseMotionListener, Runnable 
    { 
         // Define your thread. 
         Thread clockThread; 
         // This textfield will show the time. 
         TextField clockField; 
         // Date will give us the current hours, minutes and seconds 
         Date date; 
         // This variable will remain true for as long 
         // we want the thread to run. 
         boolean running = true;
         
       

         URL base;

         MediaTracker mt;
         Image test;
         public void init() 
         { 
        	 addMouseMotionListener((MouseMotionListener)this);
        	 addKeyListener((KeyListener)this);

        	 mt = new MediaTracker(this);
        	 LevelArray.loadAI();
        	 try { 
        		 base = getDocumentBase(); 
        		 Variables.directory = base;
        	 } 
        	 catch (Exception e) {}

        	 Variables.SpriteSheet = getImage(base,"files/SpriteSheet.PNG");
        	 Variables.player = getImage(base,"files/player.PNG");
        	 Variables.ScreenOverlay = getImage(base,"files/GUI/ScreenOverlay.png");

        	 mt.addImage(Variables.SpriteSheet,1);

        	
        	 try { 
        		 mt.waitForAll(); 
        	 } 
        	 catch (InterruptedException  e) {
        		 
        	 }
        	 
        	 int i = 0;
     		for(int x = 0; x < 50; x++){
     			for(int y = 0; y < 50; y++){
     				i++;
     				Variables.TileTypes[i] = createImage(new FilteredImageSource(Variables.SpriteSheet.getSource(),new CropImageFilter(y*32,x*32,32,32))); 
     				
     				//Variables.TileTypes[i] = ((null) Variables.SpriteSheet).getSubimage(y*32,x*32,32,32);
     			}
     		}
        	 LevelArray.blanklvl();
        	 clockThread= new Thread(this); 
        	 clockThread.start(); 

        	 LevelArray.loadLevel(Variables.currentLvl);
        	 Variables.isPlaying = true;
         }
         
         public void update(Graphics g){
        	 paint(g);
         }
         Image dbImage;
         Graphics dbg;
         public void paint(Graphics g){
        	 dbImage = createImage(getWidth(), getHeight());
        	 dbg = dbImage.getGraphics();
        	 screenGraphics.paintScreen(dbg);
        	 g.drawImage(dbImage, 0, 0, this);
         }
        
         // Very important. You do not want your thread to keep running when 
         // the applet is deactivated (eg. user left page) 
         public void destroy() 
         { 
              // will cause thread to stop looping 
              running = false; 
              // destroy it. 
              clockThread = null; 
         } 
 

     // The method that will be called when you have a thread. 
     // You always need this when you implement Runnable (use a thread)
         
         
     public void run() 
     { 
          // loop until told to stop 
          while (running) 
          { 
               // Construct the current date.
        	  Player.checkPos();
               repaint();
               
               //Now the reason for threads 
               try 
               { 
                     // Wait 500milliseconds before continuing 
                    clockThread.sleep(25); 
               } 
               catch (InterruptedException e) 
               { 
                    System.out.println(e); 
                } 
               // he has wait and will now restart his actions. 
          } 
     }
	@Override
	public void mouseDragged(MouseEvent e) {
		Variables.mouseX = e.getX();
		Variables.mouseY = e.getY();
		
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		Variables.mouseX = e.getX();
		Variables.mouseY = e.getY();
	}

	@Override
	public void keyPressed(KeyEvent e) {

		Player.movePlayer(e.getKeyCode());
		GameSettings.toggleStatusBar(e.getKeyCode());
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}